#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <mqueue.h>

#include "config.h"
#include "balr_destockage.h"


void* BALRdestockage()
{
	// Initialisation -----------------------------------------------
	mqd_t queue_versDestockage;
	int reading_size;
	MSG_versDestockage msg_versDestockage;

	// opening "BAL_versDestockage", READ only
	queue_versDestockage = mq_open(BAL_versDestockage, O_RDONLY);
	if(queue_versDestockage == -1)
	{
		perror("[BALR_Destockage] Error opening BAL_versDestockage:");
		return 1;
	}



	// Running loop -------------------------------------------------

	for(;;)
	{
		// waiting for a msg
		reading_size = mq_receive(queue_versDestockage, (const char *) &msg_versDestockage, sizeof(MSG_versDestockage), NULL);
		
		if(reading_size == -1)
		{
			perror("[BAL_Destockage] Failed to receive msg from BAL_versDestockage:");
		}
		else
		{
			// Reading the msg
			int nbA = msg_versDestockage.nbPaletteA;
			int nbB = msg_versDestockage.nbPaletteB;

			printf("[BAL_Destockage] Received order to destock:\n- nb pal A = %d\n- nb pal B = %d\n\n", nbA, nbB);
		}
	}
}
